package com.example.administrator.runbook.findfragment.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.runbook.R;
import com.example.administrator.runbook.findfragment.model.ClassifyEvent;
import com.example.administrator.runbook.findfragment.model.FindAllClassify;
import com.example.administrator.runbook.findfragment.model.FindClassify;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by 俊峰 on 2016/11/8.
 */

public class FindClassifyAdapter extends SectionedRecyclerViewAdapter<HeaderHolder, DescHolder, RecyclerView.ViewHolder> {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<FindAllClassify> mAllClassifies;
    private SparseBooleanArray mBooleanMap;

    public FindClassifyAdapter(Context context, List<FindAllClassify> allClassifies) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(this.mContext);
        mAllClassifies = allClassifies;
        mBooleanMap = new SparseBooleanArray();
    }

    public void setData(List<FindAllClassify> Classifies){
        mAllClassifies.clear();
        mAllClassifies.addAll(Classifies);
        notifyDataSetChanged();
    }

    @Override
    protected int getSectionCount() {
        int ret = 0;
        if (mAllClassifies != null) {
            ret = mAllClassifies.size();
        }
        return ret;
    }

    @Override
    protected int getItemCountForSection(int section) {
        int ret = 0;
        if (mAllClassifies.get(section).getFindEntryList() != null) {
            ret = mAllClassifies.get(section).getFindEntryList().size();
            if (ret >= 6 && !mBooleanMap.get(section)) {
                ret = 6;
            }
        }
        return ret;
    }

    @Override
    protected boolean hasFooterInSection(int section) {
        return false;
    }

    @Override
    protected HeaderHolder onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) {
        return new HeaderHolder(mLayoutInflater.inflate(R.layout.find_title_item, parent, false));
    }

    @Override
    protected RecyclerView.ViewHolder onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    protected DescHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        return new DescHolder(mLayoutInflater.inflate(R.layout.find_desc_item, parent, false));
    }

    @Override
    protected void onBindSectionHeaderViewHolder(final HeaderHolder holder, final int section) {
        holder.getItemShowTitle().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isOpen = mBooleanMap.get(section);
                String text = isOpen ? "展开" : "关闭";
                mBooleanMap.put(section, !isOpen);
                holder.getItemShowTitle().setText(text);
                notifyDataSetChanged();
            }
        });

        FindAllClassify classify = mAllClassifies.get(section);
        holder.getItemDescTitle().setText(classify.getName());
        holder.getItemShowTitle().setText(mBooleanMap.get(section) ? "关闭" : "展开");
    }

    @Override
    protected void onBindSectionFooterViewHolder(RecyclerView.ViewHolder holder, int section) {

    }

    @Override
    protected void onBindItemViewHolder(DescHolder holder, final int section, final int position) {
        FindClassify.FindEntry entry = mAllClassifies.get(section).getFindEntryList().get(position);
        //字体加粗（有效）{ desc.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); // 经测试该方法无效 }
        TextView desc = holder.getItemDesc();
        TextPaint paint = desc.getPaint();
        paint.setFakeBoldText(true);
        desc.setText(entry.getClassName());
        RelativeLayout layout = holder.getRelativeLayout();
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FindClassify.FindEntry event = mAllClassifies.get(section).getFindEntryList().get(position);
                ClassifyEvent classifyEvent = new ClassifyEvent();
                classifyEvent.setItem(event);
                classifyEvent.setTitle(mAllClassifies.get(section).getName());
                EventBus.getDefault().post(classifyEvent);
            }
        });
        TextView number = holder.getItemNumber();
        number.setText(entry.getClassNumber() + " 本");
    }
}
